package com.twu.biblioteca;

import com.twu.biblioteca.actions.*;
import com.twu.biblioteca.controllers.BookController;
import com.twu.biblioteca.controllers.MovieController;
import com.twu.biblioteca.controllers.UserController;
import com.twu.biblioteca.db.Database;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Launcher {
    private UserController userController;
    private BookController bookController;
    private MovieController movieController;

    private List<Action> actions;

    private Scanner scanner;

    public Launcher() {
        Database database = new Database();
        database.init();

        userController = new UserController(database);
        bookController = new BookController(database);
        movieController = new MovieController(database);

        actions = new ArrayList<Action>();
        actions.add(new SelectBook(userController, bookController, scanner));
        actions.add(new ViewBooks(bookController));
        actions.add(new ViewMovies(movieController));
        actions.add(new Authenticate(userController, scanner));
        actions.add(new ViewMemberDetails(userController));
    }

    public Launcher(UserController userController) {
        this();
        this.userController = userController;
    }

    public void start() {
        System.out.println("Welcome to the Biblioteca application!");

        while (true) {
            showOptionsMenu();
            selectOption();
        }
    }

    public void showOptionsMenu() {
        System.out.println();

        int option = 1;
        for (Action action : actions) {
            System.out.println(option + " - " + action.displayName());
            option++;
        }
    }

    public void selectOption() {
        System.out.print("Select an option: ");

        scanner = new Scanner(System.in);
        int option = scanner.nextInt();

        System.out.println();

        if (option < 1 || option > actions.size()) {
            System.out.println("Select a valid option!!");

        } else {
            actions.get(option - 1).perform();
        }
    }
}
